using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Inventory.Domain.Models;
using Inventory.Database;

namespace Inventory.Database.Models
{ 
    public class ProfessionRepository : IProfessionRepository
    {
        private readonly InventoryContext _context;

        public ProfessionRepository(UnitOfWork unitOfWork)
        {
            _context = unitOfWork.Context;
        }

        public IQueryable<Profession> All
        {
            get { return _context.Professions; }
        }

        public IQueryable<Profession> AllIncluding(params Expression<Func<Profession, object>>[] includeProperties)
        {
            IQueryable<Profession> query = _context.Professions;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Profession Find(int id)
        {
            return _context.Professions.Find(id);
        }

        public void InsertOrUpdate(Profession profession)
        {
            if (profession.Id == default(int)) {
                // New entity
                _context.Professions.Add(profession);
            } else {
                // Existing entity
                _context.Entry(profession).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var profession = _context.Professions.Find(id);
            _context.Professions.Remove(profession);
        }

        public void Save()
        {
            _context.SaveChanges();
        }

        public void Dispose() 
        {
            _context.Dispose();
        }
    }

    public interface IProfessionRepository : IDisposable
    {
        IQueryable<Profession> All { get; }
        IQueryable<Profession> AllIncluding(params Expression<Func<Profession, object>>[] includeProperties);
        Profession Find(int id);
        void InsertOrUpdate(Profession profession);
        void Delete(int id);
        void Save();
    }
}